package com.neu.wms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.wms.dto.WarehouseDto;
import com.neu.wms.entity.WmsCustomer;
import com.neu.wms.entity.WmsWarehouse;
import com.neu.wms.service.WarehouseService;
import com.neu.wms.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {
    @Autowired
    WarehouseService warehouseService;

    @PostMapping("queryAll")
    public ResultVo selectAll(@RequestBody WarehouseDto warehouseDto)
    {
        PageHelper.startPage(warehouseDto.getCurrentPage(),warehouseDto.getPageSize(),true);
        List<WmsWarehouse> warehouses = warehouseService.selectAll(warehouseDto);
        PageInfo<WmsWarehouse> pageInfo = new PageInfo<>(warehouses);
        return ResultVo.ok().data("list",warehouses).data("total",pageInfo.getTotal());
    }

    @GetMapping("{id}")
    public ResultVo queryById(@PathVariable("id") Long id){
        WmsWarehouse wmsWarehouse = warehouseService.selectByPrimaryKey(id);
        return ResultVo.ok().data("warehouse",wmsWarehouse);
    }

    @PostMapping("")
    public ResultVo add(@RequestBody @Valid WmsWarehouse warehouse)
    {
        warehouse.setStatus(0);
        int i = warehouseService.insert(warehouse);
        if (i>0) {
            return ResultVo.ok("添加成功");
        }else {
            return ResultVo.error("添加失败");
        }
    }

    @PutMapping("")
    public ResultVo update(@RequestBody WmsWarehouse warehouse)
    {
        int i = warehouseService.update(warehouse);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }

    @PutMapping("/quit")
    public ResultVo updateDelFlag(@RequestBody WarehouseDto warehouseDto)
    {
        if(warehouseDto.getStatus()==null)
            throw new IllegalArgumentException("Argument cannot be null or empty");
        int i = warehouseService.updateDelFlag(warehouseDto);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }

    @DeleteMapping("/{id}")
    public ResultVo delete(@PathVariable Long id)
    {
        int i = warehouseService.delete(id);
        if (i>0) {
            return ResultVo.ok("删除成功");
        }else {
            return ResultVo.error("删除失败");
        }
    }
}
